package com.firegod.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.firegod.commonutils.result.ResultCode;
import com.firegod.commonutils.vo.ResultVo;
import com.firegod.servicebase.exceptionhandler.exception.GuliException;
import com.firegod.teacher.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "教师管理")
@RestController
@RequestMapping("/teacher")
@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/login")
    @ApiOperation("教师登录")
    public ResultVo login(@RequestBody JSONObject jsonObject) {
        String teacher = jsonObject.getString("userId");
        String password = jsonObject.getString("password");
        if (StringUtils.isBlank(teacher)) {
            throw GuliException.from(ResultCode.NO_TEACHER_ID_ERROR);
        } else if (StringUtils.isBlank(password)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        }
        String token = teacherService.login(teacher, password);
        return ResultVo.ok().data("token", token);
    }

    @PostMapping("/register")
    @ApiOperation("教师注册")
    public ResultVo register(@RequestBody JSONObject jsonObject) {
        String teacherId = jsonObject.getString("userId");
        String password = jsonObject.getString("password");
        if (StringUtils.isBlank(teacherId)) {
            throw GuliException.from(ResultCode.NO_TEACHER_ID_ERROR);
        } else if (StringUtils.isBlank(password)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        }
        String token = teacherService.register(teacherId, password);
        return ResultVo.ok().data("token", token);
    }

    @PostMapping("/create")
    @ApiOperation("教师创建")
    public ResultVo create(@RequestBody JSONObject jsonObject) {
        String teacherId = jsonObject.getString("userId");
        if (StringUtils.isBlank(teacherId)) {
            throw GuliException.from(ResultCode.NO_TEACHER_ID_ERROR);
        }
        teacherService.create(teacherId);
        return ResultVo.ok();
    }

    @PostMapping("/changePassword")
    @ApiOperation("教师修改密码")
    public ResultVo changePassword(@RequestBody JSONObject jsonObject) {
        String oldPassword = jsonObject.getString("oldPassword");
        String password = jsonObject.getString("password");
        if (StringUtils.isBlank(oldPassword)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        } else if (StringUtils.isBlank(password)) {
            throw GuliException.from(ResultCode.NO_PASSWORD_ERROR);
        }
        teacherService.changePassword(oldPassword, password);
        return ResultVo.ok();
    }

    @GetMapping("/teacherInfo")
    @ApiOperation("教师信息")
    public ResultVo teacherInfo() {
        return ResultVo.ok().data(teacherService.getTeacherInfo());
    }

    @PostMapping("/changeUserInfo")
    @ApiOperation("修改教师信息")
    public ResultVo changeTeacherInfo(@RequestBody JSONObject jsonObject) {
        String mobile = jsonObject.getString("mobile");
        String email = jsonObject.getString("email");
        String name = jsonObject.getString("name");
        teacherService.changeInfo(mobile, email, name);
        return ResultVo.ok();
    }

    @GetMapping("/logout")
    @ApiOperation("教师登出")
    public ResultVo logout() {
        teacherService.logout();
        return ResultVo.ok();
    }

    @GetMapping("/getAllCourses")
    @ApiOperation("获取该教师所有课程")
    public ResultVo getAllCourses() {
        return ResultVo.ok().data("courseList", teacherService.getAllCourses());
    }

    @PostMapping("/getAll")
    @ApiOperation("获取所有老师")
    public ResultVo getAllTeacher(@RequestBody JSONObject jsonObject) {
        Integer limit = jsonObject.getInteger("limit");
        Integer offset = jsonObject.getInteger("offset");
        return ResultVo.ok().data(teacherService.getAllTeacher(offset - 1, limit));
    }

    @GetMapping("/recommendTeacher/{limit}")
    @ApiOperation("获取推荐老师")
    public ResultVo getRecommendTeacher(@PathVariable Integer limit) {
        if (limit == null) {
            throw GuliException.from("请输入要获取的推荐老师的数量！");
        }
        return ResultVo.ok().data(teacherService.getRecommendTeacher(limit));
    }

    @GetMapping("/myCourse")
    @ApiOperation("教师选课信息")
    public ResultVo getCourses() {
        return ResultVo.ok().data("courseList", teacherService.getCourse());
    }
}
